package com.wing.order.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.order.model.entity.InventoryJobLabel;
import com.wing.order.model.response.OrderReceiveVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.OrderReceive;
//import com.wing.order.model.response.OrderReceiveVO;
import com.wing.order.service.OrderReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.order.model.request.OrderReceiveForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-领货单管理", tags = "领货单管理")
@RestController("appOrderReceiveController")
@RequestMapping("order/app/orderReceive")
@Slf4j
public class OrderReceiveController {

    @Autowired
    private OrderReceiveService orderReceiveService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "待上货列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderReceiveVO> list(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order",  defaultValue = "desc", required = false) String order
            ,String code
            ,String queryFlag
    ) {
        Page<OrderReceiveVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("queryFlag",queryFlag);
        Page<OrderReceiveVO> list = orderReceiveService.pageListLoading4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "领货列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listReceive")
    public PageResult<OrderReceiveVO> listReceive(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order",  defaultValue = "desc", required = false) String order
            ,String code
            ,String queryFlag
    ) {
        Page<OrderReceiveVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("queryFlag",queryFlag);
        Page<OrderReceiveVO> list = orderReceiveService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long orderReceiveId) {
        OrderReceiveVO orderReceiveVO = orderReceiveService.get4appById(orderReceiveId);
        if (orderReceiveVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), orderReceiveVO);
    }

    @PostMapping()
    public JsonResult add(@Validated OrderReceiveForm orderReceiveForm) {
        if (orderReceiveService.add(orderReceiveForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "领货提交")
    @PostMapping("/receiveSubmit")
    public JsonResult receiveSubmit(@RequestBody List<InventoryJobLabel> list){
        try{
            orderReceiveService.receiveSubmit(list);
            return JsonResult.success("领货提交成功");
        }catch (Exception e){
            log.error("receive submit fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "添加领货单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/save")
    public JsonResult save(@Validated(OrderReceiveForm.Add.class)@RequestBody OrderReceiveForm orderReceiveForm) {
        try{
            orderReceiveService.save4app(orderReceiveForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("receive fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

}
